/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package units;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.collections.FastHashMap;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class GeneLibrary {

    private FastHashMap genes = new FastHashMap();

    public Gene getGene(int id){
        if(!genes.containsKey(id)){
            Gene g = new Gene(id);
            genes.put(id, g);
            return g;
        } else {
            return (Gene) genes.get(id);
        }
    }

    public void populateGeneSymbols(){
        Handle handle = Handle.getConnectedHandle("ncbi");
        for(Object g : genes.values()){
            Gene gene = (Gene) g;
            String symbol = getGeneSymbol(gene.getId(), handle);
//            System.out.println(symbol);
            gene.setSymbol(symbol);
        }
        handle.closeConnection();
    }

    public static String getGeneSymbol(int id, Handle h) {

        String sql = "select symbol from ncbi.gene_info where gId="+id;
        ResultSet rs = h.runSQLquery(sql);
        String symbol = "gId_"+id;
        try {
            if(rs.next()){
                symbol = rs.getString("symbol");
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return symbol;
    }

}